﻿#region License
// Copyright (c) 2009 Shengmin Zhang
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion
using System;
using System.Collections.Generic;
using System.Text;

namespace Apprender.Web
{
    /// <summary>
    /// this is the base class for building tags, which provides useful helper methods.
    /// please inherit from HtmlBuilder or from this class.
    /// </summary>
    public class TagBuilder //inspired by ASP.NET MVC HtmlHelper
    {
        public TagBuilder() { }

        private const int _size = 30;

        /// <summary>
        /// returns html strings
        /// </summary>
        /// <param name="tag">tag name</param>
        /// <param name="attributes">attribute pairs</param>
        /// <param name="tagType"></param>
        /// <param name="innerHtml">html in between the begin and end tag</param>
        /// <returns>html strings</returns>
        public string Render(string tag, IDictionary<string, string> attributes, TagType tagType, string innerHtml)
        {
            StringBuilder sb = new StringBuilder(attributes.Count * _size);
            this.Populate(tag, attributes, tagType, sb, innerHtml);
            return sb.ToString();
        }

        /// <summary>
        /// fill the StringBuilder with tag
        /// </summary>
        /// <param name="tag">tag name</param>
        /// <param name="attributes">attribute pairs</param>
        /// <param name="tagType"></param>
        /// <param name="sb"></param>
        /// <param name="innerHtml">html in between the begin and end tag</param>
        public void Populate(string tag, IDictionary<string, string> attributes, TagType tagType, StringBuilder sb, string innerHtml)
        {
            this.PopulateWithBeginTag(sb, tag);
            this.PopulateWithAttributes(sb, attributes);
            this.PopulateWithEndTag(sb, tag, tagType, innerHtml);
        }

        /// <summary>
        /// fill the StringBuilder with the begin tag
        /// </summary>
        /// <param name="sb"></param>
        /// <param name="tag">tag name</param>
        protected void PopulateWithBeginTag(StringBuilder sb, string tag)
        {
            sb.Append("<");
            sb.Append(tag);
            sb.Append(" ");
        }

        /// <summary>
        /// fill the StringBuilder with end tag
        /// </summary>
        /// <param name="sb"></param>
        /// <param name="tag">tag name</param>
        /// <param name="tagType"></param>
        /// <param name="innerHtml">html in between the begin and end tag</param>
        protected void PopulateWithEndTag(StringBuilder sb, string tag, TagType tagType, string innerHtml)
        {
            switch (tagType)
            {
                case TagType.Normal:
                    sb.Append(">");
                    sb.Append(innerHtml);
                    sb.Append("</");
                    sb.Append(tag);
#if DEBUG
                    sb.AppendLine(">");
#else
                    sb.Append(">");
#endif
                    break;

                case TagType.SelfClosing:
#if DEBUG
                    sb.AppendLine("/>");
#else
                    sb.Append("/>");
#endif

                    break;
            }
        }

        /// <summary>
        /// fill the StringBuilder with attributes
        /// </summary>
        /// <param name="sb"></param>
        /// <param name="attributes">attribute pairs</param>
        protected void PopulateWithAttributes(StringBuilder sb, IDictionary<string, string> attributes)
        {
            foreach (var attr in attributes)
            {
                sb.Append(attr.Key);
                sb.Append(@"=""");
                sb.Append(attr.Value);
                sb.Append(@""" ");
            }
        }
    }
}
